package com.zw.admin.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.zw.admin.server.page.table.PageTableRequest;
import com.zw.admin.server.page.table.PageTableHandler;
import com.zw.admin.server.page.table.PageTableResponse;
import com.zw.admin.server.page.table.PageTableHandler.CountHandler;
import com.zw.admin.server.page.table.PageTableHandler.ListHandler;
import com.zw.admin.server.dao.FarmWeatherDao;
import com.zw.admin.server.model.FarmWeather;


import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/farmWeathers")
public class FarmWeatherController {

    @Autowired
    private FarmWeatherDao farmWeatherDao;



    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplatee;

    @GetMapping("/shuai")
    public List<FarmWeather> redisTest(){
        List<FarmWeather> list = farmWeatherDao.getWeatherList();
        return list;
    }

    @PostMapping
    @ApiOperation(value = "保存")
    public FarmWeather save(@RequestBody FarmWeather farmWeather) {
        farmWeatherDao.save(farmWeather);

        return farmWeather;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public FarmWeather get(@PathVariable Long id) {
        return farmWeatherDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public FarmWeather update(@RequestBody FarmWeather farmWeather) {
        farmWeatherDao.update(farmWeather);

        return farmWeather;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return farmWeatherDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<FarmWeather> list(PageTableRequest request) {
                return farmWeatherDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }



    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        farmWeatherDao.delete(id);
    }



    @RequestMapping("/aaa")
    public String sayHello() {
        return "helloword";

    }


    @RequestMapping(value = "/fu")
    @ResponseBody
    public List<FarmWeather> fuzzyCUser(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        List<FarmWeather> list = farmWeatherDao.select(result);
        //  result.put("list",list);
        request.getSession().setAttribute("LIST", list);
        //  return result;
        return list;

    }

    @ApiOperation(value = "nice")
     @GetMapping("/nice")
     public  String getUsers(){
        long startTime;
        long endTime;
        startTime = System.currentTimeMillis();
        //获取redis中key="userJSON"的value
        String weatherJSON = (String)redisTemplate.opsForValue().get("weatherJSON");
        if(weatherJSON!=null){
            endTime = System.currentTimeMillis();
            System.out.println("取缓存成功，耗时" + (endTime-startTime) + "ms");
            return weatherJSON;
        }

        //在redis中获取不到则执行以下代码
        startTime = System.currentTimeMillis();
        List<FarmWeather> list = farmWeatherDao.getWeatherList();
        //将userList转为json字符串
        weatherJSON = JSONArray.fromObject(list).toString();
        endTime = System.currentTimeMillis();
        //将查询结果存入redis中作缓存
        //四个参数从左至右分别为：key，value，有效时间，时间单位
        //这里即：创建一个key="userJSON",value=#{userJSON}的键值对，其有效时间是10秒
        redisTemplate.opsForValue().set("weatherJSON",weatherJSON,10, TimeUnit.SECONDS);
        System.out.println("取缓存失败，耗时" + (endTime-startTime) + "ms");
        return weatherJSON;
    }



}
